<?php

namespace App\Http\Requests\Seller\GoodsType;

use App\Models\GoodsType;
use Illuminate\Foundation\Http\FormRequest;

class CreateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => ['required', 'bail'],
            'parent_id' => ['required', function ($attribute, $value, $fail) {
                $type = GoodsType::find($value);
                if($value!=0){
                    if(!$type){
                        $fail(':attribute是无效的');
                    }else if(!$type->is_parent){
                        $fail(':attribute是无效的');
                    }
                }


            },'bail'],
            'status' => ['required', 'boolean', 'bail'],
            'is_parent'=>['required','boolean','bail']
        ];
    }

    public function attributes()
    {
        return [
            'name' => '分类名称',
            'is_parent'=>'是否是父级'
        ];
    }
}
